const {
    queryAllArticlesDB,
    queryArticleOnIDDB,
    queryArticlesOnCateDB,
    addArticleDB,
    editArticleDB,
    deleteArticleDB
} = require('../db/article');
const { queryCategoriesDB } = require('../db/articleCate');


const queryAllArticles = async () => {
    try {
        //获取数据库内容
        const dbRes = await queryAllArticlesDB();
        const cates = await queryCategoriesDB();
        //拼装数据
        return dbRes.map(each => {
            each.cate = cates.filter(cate => cate.cateID === each.cateID)[0];
            delete each.cateID;
            each.content = each.content.split('。')[0].substring(0, 100);
            each.omit = true;
            return each;
        });
    } catch (err) {
        console.log(err);
        throw err;  //继续抛出
    }
}

const queryArticleOnID = async (articleID) => {
    try {
        articleID = Number.parseInt(articleID);
        //获取数据库内容
        const dbRes = await queryArticleOnIDDB(articleID);
        const cates = await queryCategoriesDB();
        //拼装数据
        dbRes.cate = cates.filter(cate => cate.cateID == dbRes.cateID)[0];
        dbRes.omit = false;
        delete dbRes.cateID;
        return dbRes;
    } catch (err) {
        console.log(err);
        throw err;  //继续抛出
    }
}

const queryArticlesOnCate = async (cateID) => {
    try {
        //获取数据库内容
        const dbRes = await queryArticlesOnCateDB(cateID);
        const cates = await queryCategoriesDB();
        //拼装数据
        return dbRes.map(each => {
            each.cate = cates.filter(cate => cate.cateID === each.cateID)[0];
            delete each.cateID;
            each.content = each.content.split('。')[0].substring(0, 100);
            each.omit = true;
            return each;
        });
    } catch (err) {
        console.log(err);
        throw err;  //继续抛出
    }
}

//新增文章
const addArticle = async (title, markdown, cateID) => {
    try {
        //更新返回数据
        const dbRes = await addArticleDB(title, markdown, cateID);
        console.log('数据库返回：', dbRes)
        if (dbRes.affectedRows === 1) {
            //插入成功
            return true;
        }
        throw new Error(message = JSON.stringify(dbRes))
    } catch (err) {
        console.log(err);
        throw err;
    }
}

//编辑文章
const editArticle = async (title, markdown, cateID, articleID) => {
    try {
        const dbRes = await editArticleDB(articleID, title, markdown, cateID);
        if (dbRes.affectedRows === 1) {
            //编辑成功
            return true;
        }
        throw new Error(message = JSON.stringify(dbRes))
    } catch (err) {
        console.log(err)
        throw err;
    }
}


//删除文章
const deleteArticle = async (articleID) => {
    try {
        const dbRes = await deleteArticleDB(articleID);
        if (dbRes.affectedRows === 1) {
            //删除成功
            return true;
        }
        throw new Error(message = JSON.stringify(dbRes))
    } catch (err) {
        console.log(err)
        throw err;
    }
}
exports.queryAllArticles = queryAllArticles;
exports.queryArticleOnID = queryArticleOnID;
exports.queryArticlesOnCate = queryArticlesOnCate;
exports.addArticle = addArticle;
exports.editArticle = editArticle;
exports.deleteArticle = deleteArticle;
